package cm.mefobe.android.telecomcost.activities;

import java.util.ArrayList;
import java.util.Calendar;

import cm.mefobe.android.telecomcost.R;
import cm.mefobe.android.telecomcost.TelecomCost;
import cm.mefobe.android.telecomcost.Utility;
import cm.mefobe.android.telecomcost.adapter.DailyThresholdAdapter;
import cm.mefobe.android.telecomcost.dialog.DailyThresholdDialog;
import cm.mefobe.android.telecomcost.entities.DailyThreshold;
import cm.mefobe.android.telecomcost.entities.MonthlyThreshold;
import cm.mefobe.android.telecomcost.entities.WeeklyThreshold;
import cm.mefobe.android.telecomcost.view.MonthlyThresholdSpinner;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class DailyThresholdListActivity extends TelecomcostActivity {

	private ListView threshold_list;
	private ArrayList<DailyThreshold> thresholds;	
	private MonthlyThresholdSpinner monthly_threshold_list;
	private MonthlyThreshold currentMonth;
	private WeeklyThreshold currentWeek;

	private Spinner week_list;

	public ArrayList<DailyThreshold> getThresholds() {
		return thresholds;
	}

	public void setThresholds(ArrayList<DailyThreshold> thresholds) {
		this.thresholds = thresholds;
	}

	public static final int DIALOG_NEW_THRESHOLD = 1;

	public Dialog onCreateDialog(int id) {
		Log.i("DailyThresholdListActivity", "Creating dialog ohne bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_NEW_THRESHOLD:
			dialog = new DailyThresholdDialog(DailyThresholdListActivity.this);
			break;
		default:
			dialog = null;
		}

		return dialog;
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		Log.i("DailyThresholdListActivity", "Creating dialog with bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_NEW_THRESHOLD:
			dialog = new DailyThresholdDialog(DailyThresholdListActivity.this);
			break;
		default:
			dialog = null;
		}

		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		Log.i("DailyThresholdListActivity", "Preparing dialog with bundle");
		super.onPrepareDialog(id, dialog, args);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		Log.i("DailyThresholdListActivity", "Preparing dialog ohne bundle");
		super.onPrepareDialog(id, dialog);
		Utility.setDialogLayout(dialog, LayoutParams.WRAP_CONTENT, 99);
	}

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("DailyThresholdListActivity", "Creating the activity");

		super.onCreate(savedInstanceState);
		TelecomCost.getDatabaseManager();

		setContentView(R.layout.daily_threshold_list);

		threshold_list = (ListView) findViewById(R.id.listViewThreshold);
		monthly_threshold_list = (MonthlyThresholdSpinner) findViewById(R.id.spinnerMonths);
		week_list = (Spinner) findViewById(R.id.spinnerWeeksOfMonth);

		week_list.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				setWeek(arg2);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		monthly_threshold_list
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						setMonth(arg2);
					}

					public void onNothingSelected(AdapterView<?> arg0) {
					}
				});
	}

	protected void setMonth(int index) {
		currentMonth = monthly_threshold_list.getThresholds().get(index);
		bindWeeks();
	}

	protected void setWeek(int index) {
		currentWeek = currentMonth.getWeeklyThresholds().get(index);
		bindThresholds();
	}

	public WeeklyThreshold getWeek() {
		return currentWeek;
	}

	public MonthlyThreshold getMonth() {
		return currentMonth;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("DailyThresholdListActivity",
				"creating options menu for providers : ");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_thresholds, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.i("DailyThresholdListActivity", "onOptionsItemSelected");
		switch (item.getItemId()) {
		case R.id.menu_item_edit_thresholds:
			showDialog(DIALOG_NEW_THRESHOLD);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i("DailyThresholdListActivity", "onActivityResult");
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		Log.i("DailyThresholdListActivity", "Destroying the activity");
		super.onDestroy();
		threshold_list = null;
	}

	@Override
	protected void onPause() {
		Log.i("DailyThresholdListActivity", "Pausing the activity");
		super.onPause();
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		Log.i("DailyThresholdListActivity", "Postcreating the activity");
		super.onPostCreate(savedInstanceState);
	}

	@Override
	protected void onPostResume() {
		Log.i("DailyThresholdListActivity", "PostResuming the activity");
		super.onPostResume();
		bindData();
	}

	public void bindData() {
		bindMonthlyThresholds();
		selectCurrentMonth();
		//bindWeeks();
	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Log.i("DailyThresholdListActivity",
				"activity has been attached to the window manager");
	}

	@Override
	protected void onRestart() {
		Log.i("DailyThresholdListActivity", "Restarting the activity");
		super.onRestart();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.i("DailyThresholdListActivity",
				"onRestoreInstanceState the activity");
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		Log.i("DailyThresholdListActivity", "Resuming the activity");
		super.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.i("DailyThresholdListActivity", "onSaveInstanceState the activity");
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onStart() {
		Log.i("DailyThresholdListActivity", "Starting the activity");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.i("DailyThresholdListActivity", "Stopping the activity");
		super.onStop();
	}

	public void bindThresholds() {
		if (currentMonth != null) {
			Log.i("DailyThresholdListActivity",
					"binding DailyThreshold...we get the current weekly threshold from the monthly threshold");
			currentWeek = currentWeek == null ? currentMonth.getWeeklyThresholds().get(0) : currentWeek;
			
			thresholds = currentWeek.getDailyThresholds();

			threshold_list.setAdapter(new DailyThresholdAdapter(
					DailyThresholdListActivity.this, R.layout.threshold_view,
					thresholds, this));
		} else {
			Log.i("DailyThresholdListActivity",
					"cannot bind the daily threshold...set the month and week first");
		}
	}

	public void setMonthlyThreshold(MonthlyThreshold threshold)
			throws Exception {
		if (monthly_threshold_list.getThresholds() != null) {
			int index = 0;
			while (index < monthly_threshold_list.getThresholds().size()) {
				MonthlyThreshold _threshold = monthly_threshold_list.getThresholds().get(index);
				if (_threshold.getMonth() == threshold.getMonth()) {
					_threshold = threshold;					
					return;
				}

				index++;
			}
		}

		throw new Exception(
				"could not set the monthly threshold...does not exist in the list");
	}

	public void bindMonthlyThresholds() {			
		monthly_threshold_list.setAdapter(null);
	}

	private void bindWeeks() {
		if (currentMonth != null) {
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(
					DailyThresholdListActivity.this,
					android.R.layout.simple_spinner_item,
					currentMonth.getWeeksArrayAdapter());
			week_list.setAdapter(adapter);
		} else {
			Log.i("MonthlyThresholdListActivity",
					"cannot bind the weeks...current month not set");
		}
	}

	private void selectCurrentMonth() {
		Log.i("MonthlyThresholdListActivity", "selecting current month...");
		if (monthly_threshold_list.getThresholds() != null && monthly_threshold_list.getThresholds().size() >= 12) {
			Calendar cal = Calendar.getInstance();
			int month = cal.get(Calendar.MONTH);

			int index = 0;
			while (index < monthly_threshold_list.getThresholds().size()) {
				MonthlyThreshold threshold = monthly_threshold_list.getThresholds().get(index);
				if (threshold.getMonth().getMonthOfYear() == month) {
					Log.i("MonthlyThresholdListActivity",
							"selecting current month..."
									+ threshold.getMonthNameLong());
					currentMonth = threshold;
					monthly_threshold_list.setSelection(index);					
					return;
				}

				index++;
			}

			Log.i("MonthlyThresholdListActivity",
					"could not select the current month...");

		} else {
			Log.i("MonthlyThresholdListActivity",
					"cannot select current month...monthly_thresholds not loaded yet");
		}
	}

	@Override
	public String toString() {
		return "DailyThresholdListActivity";
	}
}
